#include "RenderTarget.h"

RenderTarget::RenderTarget(ID3D11RenderTargetView* view, ID3D11DepthStencilView* dsv)
{
	size = 1;	
	targetsView.resize(size);
	targetsView[0] = view;
	depthStencilview = dsv;
}

RenderTarget::RenderTarget(UINT numRenderTargets)
{
	size = numRenderTargets;
	textures.resize(size);
	targetsView.resize(size);
	shaderResources.resize(size);
	depthStencilview = NULL;
}

void RenderTarget::SetDepthStencilView()
{
	// Create the depth stencil view
    /*D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
	ZeroMemory(&descDSV, sizeof(descDSV));
    descDSV.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;

	D3D11_TEXTURE2D_DESC descDepth;
    descDepth.Width = g_WindowWidth;
	descDepth.Height = g_WindowHeight;
    descDepth.MipLevels = 1;
    descDepth.ArraySize = 1;
    descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    descDepth.SampleDesc.Count = 1;
    descDepth.SampleDesc.Quality = 0;
    descDepth.Usage = D3D11_USAGE_DEFAULT;
    descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;
	result = EngineStuff::EGraphicsDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil);
	CheckErrorVoid(result);	

    result = EngineStuff::EGraphicsDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &depthStencilview[num]  );
    CheckErrorVoid(result);	*/

	///LEMBRAR: deixar os parametros do depthStencilview visiveis para personalizacao (nao ficar sempre null ...)
	depthStencilview = NULL;
}

void RenderTarget::AddTarget(int num, int texW, int texH, DXGI_FORMAT format , UINT CPUAcess ,UINT samplesCount , UINT samplesQuality )
{
	HRESULT result;
	D3D11_TEXTURE2D_DESC textureDesc;	
	D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

	ZeroMemory(&textureDesc, sizeof(textureDesc));	
	textureDesc.Width = texW;
	textureDesc.Height = texH;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = format;
	textureDesc.SampleDesc.Count = samplesCount;
	textureDesc.SampleDesc.Quality = samplesQuality;	
	textureDesc.Usage = D3D11_USAGE_DEFAULT;
	textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	textureDesc.CPUAccessFlags = CPUAcess ;
	textureDesc.MiscFlags = 0;

	// Create the render target texture.
	result = EngineStuff::EGraphicsDevice->CreateTexture2D(&textureDesc, NULL, &textures[num]);
	CheckErrorVoid(result);
	
	// Setup the description of the render target view.
	renderTargetViewDesc.Format = textureDesc.Format;
	renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
	renderTargetViewDesc.Texture2D.MipSlice = 0;

	// Create the render target view.
	result = EngineStuff::EGraphicsDevice->CreateRenderTargetView(textures[num], &renderTargetViewDesc, &targetsView[num]);
	CheckErrorVoid(result);

	// Setup the description of the shader resource view.
	shaderResourceViewDesc.Format = textureDesc.Format;
	shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
	shaderResourceViewDesc.Texture2D.MipLevels = 1;

	// Create the shader resource view.
	result = EngineStuff::EGraphicsDevice->CreateShaderResourceView(textures[num], &shaderResourceViewDesc, &shaderResources[num]);
	CheckErrorVoid(result);
	
}

RenderTarget::~RenderTarget(void)
{
	for(int i = 0 ; i < size ; i++)	
	{
		if(!shaderResources.empty()) ///talvez n tenha
		{
			if(shaderResources[i])
			{
				shaderResources[i]->Release();
				shaderResources[i] = 0;
			}
		}

		if(!textures.empty()) ///talvez n tenha
		{
			if(textures[i])
			{
				textures[i]->Release();
				textures[i] = 0;
			}
		}

		if(targetsView[i])
		{
			targetsView[i]->Release();
			targetsView[i] = 0;
		}
		if(depthStencilview)
		{
			depthStencilview->Release();
			depthStencilview = NULL;
		}
	}
}
